import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-27
 * Time: 18:38
 */
class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class Day8 {
    //100.相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null){
            return true;
        }
        if (p == null || q == null){
            return false;
        }
        if (p.val != q.val){
            return false;
        }else{
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
    }

    //572.另一棵树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null || subRoot == null){
            return false;
        }
        if (isSameTree(root,subRoot)){
            return true;
        }
        if (isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }

    //226.翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if (root == null){
            return null;
        }
        TreeNode leftTree = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(leftTree);

        return root;
    }

    //110.平衡二叉树

    /*public int height(TreeNode root){
        if (root == null){
            return 0;
        }
        return Math.max(height(root.left), height(root.right)) + 1;
    }
    public boolean isBalanced(TreeNode root) {
        if (root == null){
            return true;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);

        return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }*/
    public int height(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftheight = height(root.left);
        int rightheight = height(root.right);

        if (leftheight >= 0 && rightheight >= 0 && Math.abs(leftheight - rightheight) <= 1){
            return Math.max(leftheight, rightheight) + 1;
        }else{
            return - 1;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if (root == null){
            return true;
        }
        return height(root) >= 0;
    }

    //101.对称二叉树
    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree){
        if (leftTree == null && rightTree == null) return true;
        if (leftTree == null || rightTree == null) return false;

        if (leftTree.val != rightTree.val){
            return false;
        }
        return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;

        return isSymmetricChild(root.left ,root.right);
    }

    //102.二叉树的层序遍历
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                //System.out.println(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            lists.add(list);
        }
        return lists;
    }
}
